package com.workoutdiary.dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.workoutdiary.data.DbManager;
import com.workoutdiary.data.Table;
import com.workoutdiary.entities.MCycleRecord;
import com.workoutdiary.entities.User;

public class MCycleRecordDao {
	
	public void create(MCycleRecord record) {
		
		DbManager manager = new DbManager();
		Connection conn = manager.createConnection();
		
		PreparedStatement pst = manager.createPreparedStatement(Table.MCycleRecordTable.INSERT, conn);
		
		try {
			
			pst.setInt(1, record.getUserId());
			pst.setLong(2, record.getTimestamp());
			pst.setInt(3, record.getFase());
			pst.setString(4, record.getDescription());

			pst.execute();
			pst.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
		}
		manager.close(conn);
		
	}
	
	public List<MCycleRecord> getAll() {
		List<MCycleRecord> records = new ArrayList<MCycleRecord>();
		
		DbManager manager = new DbManager();
		Connection conn = manager.createConnection();
		
		Statement st = manager.createStatement(conn);
		try {
			ResultSet rs = st.executeQuery(Table.MCycleRecordTable.GET_ALL);
			
			while(rs.next()) {
//				MCycleRecord record = new MCycleRecord(rs.getInt(Table.MCycleRecordTable.COLUMN_USER_ID),
//						rs.getLong(Table.MCycleRecordTable.COLUMN_TIMESTAMP),
//						rs.getInt(Table.MCycleRecordTable.COLUMN_FASE),
//						rs.getString(Table.MCycleRecordTable.COLUMN_DESCRIPTION));
//				record.setId(rs.getInt(Table.MCycleRecordTable.COLUMN_ID));
				MCycleRecord record = new MCycleRecord(rs.getInt(1),
						rs.getLong(2),
						rs.getInt(3),
						rs.getString(4));
				record.setId(rs.getInt(5));
				records.add(record);
			}
			rs.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		manager.close(conn);
		manager.close(st);

		
		return records;
	}
	
	public Object[][] getRecordsMatrix(List<MCycleRecord> records) {
		records = getAll();
		Object[][] recordsMatrix= new Object[records.size()][];
		
		for (int i = 0; i < recordsMatrix.length; i++) {
			Object[] currRecord = records.get(i).getMCycleRecord();
			recordsMatrix[i] = currRecord;
		}
		
		return recordsMatrix;
	}
	
}
